package com.demo.jsf;

import java.util.ArrayList;
import java.util.Hashtable;

import javax.faces.context.FacesContext;
import javax.servlet.jsp.jstl.sql.Result;
import javax.servlet.jsp.jstl.sql.ResultSupport;

import com.demo.javabean.DBAccess;

public class AddressAction extends BaseAction {

	private String id;
	private String username;
	private String name;
	private String sex;
	private String mobile;
	private String email;
	private String qq;
	private String company;
	private String address;
	private String postcode;

	public String list() {
		// 身份验证
		if (loginAction.getUsername() == null)
			return "login";

		// 取得分页参数
		String strPageSize = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap().get("pageSize");
		if (strPageSize != null) {
			pageSize = Integer.parseInt(strPageSize);
		}
		String strPageNo = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap().get("pageNo");
		if (strPageNo != null) {
			pageNo = Integer.parseInt(strPageNo);
		}

		// 创建数据库连接
		DBAccess db = new DBAccess();
		if (db.createConn()) {

			// 查询总的记录数，计算跳页参数
			int start = pageSize * (pageNo - 1);
			// 总记录数查询SQL
			String sql1 = "select count(*) as countall from address where username='"
					+ loginAction.getUsername() + "'";
			db.query(sql1);
			if (db.next()) {
				// 计算总行数并保存
				String countall = db.getValue("countall");
				rowCount = Integer.parseInt(countall);

				// 计算总页数并保存
				pageCount = rowCount % pageSize == 0 ? rowCount / pageSize
						: rowCount / pageSize + 1;

				// 计算跳页参数并保存
				pageFirstNo = 1;// 首页
				pageLastNo = pageCount;// 尾页
				pagePreNo = pageNo > 1 ? pageNo - 1 : 1;// 前一页
				pageNextNo = pageNo < pageCount ? pageNo + 1 : pageCount;// 后一页
			}
			db.closeRs();

			// 取得当前页数据SQL
			String sql2 = "select * from address where username='"
					+ loginAction.getUsername() + "' order by name limit "
					+ start + "," + pageSize;
			list = new ArrayList<Hashtable<String, String>>();
			db.query(sql2);
			while (db.next()) {
				// 查询每行数据的各个字段数据
				Hashtable<String, String> hash = new Hashtable<String, String>();
				hash.put("id", db.getValue("id"));
				hash.put("username", db.getValue("username"));
				hash.put("name", db.getValue("name"));
				hash.put("sex", db.getValue("sex"));
				hash.put("mobile", db.getValue("mobile"));
				hash.put("email", db.getValue("email"));
				hash.put("qq", db.getValue("qq"));
				hash.put("company", db.getValue("company"));
				hash.put("address", db.getValue("address"));
				hash.put("postcode", db.getValue("postcode"));

				// 保存当前行
				list.add(hash);
			}
			db.closeRs();
			db.closeStm();
			db.closeConn();
		}

		return "addresslist";
	}

	public String add() {
		// 身份验证
		if (loginAction.getUsername() == null)
			return "login";
		
		return "addressadd";
	}

	public String insert() {
		// 身份验证
		if (loginAction.getUsername() == null)
			return "login";
		
		// 创建数据库连接
		DBAccess db = new DBAccess();
		if (db.createConn()) {

			// 组合新增SQL
			String sql = "insert into address (username, name, sex, mobile, email, qq, company, address, postcode) ";
			sql += " values('" + loginAction.getUsername() + "','" + name
					+ "','" + sex + "','" + mobile + "','" + email + "','" + qq
					+ "','" + company + "','" + address + "','" + postcode
					+ "')";

			// 转换参数编码
			try {
				sql = new String(sql.getBytes("ISO8859-1"), "UTF-8");
			} catch (Exception e) {
			}

			// 执行插入
			db.update(sql);
			db.closeStm();
			db.closeConn();
		}
		return list();
	}

	public String edit() {
		// 身份验证
		if (loginAction.getUsername() == null)
			return "login";
		
		// 取得参数
		id = FacesContext.getCurrentInstance().getExternalContext()
				.getRequestParameterMap().get("id");
		if (id == null) {
			return "addresslist";
		}

		// 创建数据库连接
		DBAccess db = new DBAccess();
		if (db.createConn()) {

			// 根据id编号查询当前行记录
			String sql = "select * from address where id=" + id;
			db.query(sql);
			if (db.next()) {
				// 取得各个字段的值并保存
				username = db.getValue("username");
				name = db.getValue("name");
				sex = db.getValue("sex");
				mobile = db.getValue("mobile");
				email = db.getValue("email");
				qq = db.getValue("qq");
				company = db.getValue("company");
				address = db.getValue("address");
				postcode = db.getValue("postcode");
			}
			db.closeRs();
			db.closeStm();
			db.closeConn();
		}
		return "addressedit";
	}

	public String update() {
		// 身份验证
		if (loginAction.getUsername() == null)
			return "login";
		
		// 创建数据库连接
		DBAccess db = new DBAccess();
		if (db.createConn()) {
			// 组合更新SQL
			String sql = "update address set name='" + name + "', sex='" + sex
					+ "', mobile='" + mobile + "', email='" + email + "', qq='"
					+ qq + "', company='" + company + "', address='" + address
					+ "', postcode='" + postcode + "' where id=" + id;

			// 转换参数编码
			try {
				sql = new String(sql.getBytes("ISO8859-1"), "UTF-8");
			} catch (Exception e) {
			}

			// 执行更新
			db.update(sql);
			db.closeStm();
			db.closeConn();
		}
		return list();
	}

	public String delete() {
		// 身份验证
		if (loginAction.getUsername() == null)
			return "login";
		
		// 取得参数
		id = FacesContext.getCurrentInstance().getExternalContext()
				.getRequestParameterMap().get("id");
		if (id == null) {
			return "addresslist";
		}
		
		// 创建数据库连接
		DBAccess db = new DBAccess();
		if (db.createConn()) {
			// 根据id组成删除SQL，执行删除
			String sql = "delete from address where id=" + id;
			db.update(sql);
			db.closeStm();
			db.closeConn();
		}
		return list();
	}

	public Result getInfo() {
		Result rs = null;
		String username = loginAction.getUsername();

		// 创建数据库连接
		DBAccess db = new DBAccess();
		if (db.createConn()) {
			// 总记录数查询SQL
			String sql1 = "select count(*) as countall from address where username='"
					+ username + "'";
			db.query(sql1);
			if (db.next()) {
				// 计算总行数并保存
				String countall = db.getValue("countall");
				rowCount = Integer.parseInt(countall);

				// 计算总页数并保存
				pageCount = rowCount % pageSize == 0 ? rowCount / pageSize
						: rowCount / pageSize + 1;

				// 计算跳页参数并保存
				pageFirstNo = 1;// 首页
				pageLastNo = pageCount;// 尾页
				pagePreNo = pageNo > 1 ? pageNo - 1 : 1;// 前一页
				pageNextNo = pageNo < pageCount ? pageNo + 1 : pageCount;// 后一页
			}
			db.closeRs();

			// 取得当前页数据SQL
			String sql2 = "select * from address where username='" + username
					+ "' order by name ";
			list = new ArrayList<Hashtable<String, String>>();
			db.query(sql2);
			rs = ResultSupport.toResult(db.getRs());

			db.closeRs();
			db.closeStm();
			db.closeConn();
		}
		return rs;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getQq() {
		return qq;
	}

	public void setQq(String qq) {
		this.qq = qq;
	}

	public String getCompany() {
		return company;
	}

	public void setCompany(String company) {
		this.company = company;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getPostcode() {
		return postcode;
	}

	public void setPostcode(String postcode) {
		this.postcode = postcode;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}
}